<template>
    <div>
        <div class="alphabet">
            <ul v-for="(items, key) of cities" :key="key" @click="handleLetterClick">
                <li class="alphabet-item">{{key}}</li>
            </ul>
        </div>

      <div class="list" ref="wrapper">
            <div>
                <div class="area">
                    <div class="title border-topbottom">当前城市</div>
                    <div class="button-list">
                        <div class="button-wrapper">
                            <div class="button">{{this.$store.state.city}}</div>
                        </div>
                    </div>
                </div>
                <div class="area">
                    <div class="title border-topbottom">热门城市</div>
                    <div class="button-list">
                        <div v-for="item of hotCities" :key="item.id">
                            <div class="button-wrapper" @click="changeCity(item.name)">
                                <div class="button">{{item.name}}</div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="area" v-for="(items, key) of cities" :key="key" :ref="key">
                    <div class="title border-topbottom">{{key}}</div>
                    <div class="item-list">
                        <div v-for="item of items" :key="item.id" @click="changeCity(item.name)">
                            <div class="item border-bottom">{{item.name}}</div>
                        </div>
                    </div>
                </div>
            </div>
      </div>
    </div>
</template>

<script>
import BScroll from 'better-scroll'
// import Alphabet from './Alphabet.vue'

export default {
//   components: { Alphabet },
  name: 'CityList',
  props: {
    // city: '',
    cities: {},
    hotCities: []
  },
  data () {
    return {
      clickAlphabet: ''
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true,
      observeDOM: true
    })
  },
  methods: {
    handleLetterClick (e) {
      this.clickAlphabet = e.target.innerText // console.log(clickAlphabet)
    },
    changeCity (newCity) {
    //   console.log(newCity)
      this.$store.dispatch('actionsChangeCity', newCity)
      this.$router.push('./')
    }
  },
  watch: {
    clickAlphabet () {
      if (this.clickAlphabet) {
        const element = this.$refs[this.clickAlphabet][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>

<style scoped>
.list{
    overflow: hidden;
    position: absolute;
    top: 82px;
    bottom: 0;
    left: 0;
    width: 100%;
}

.title{
    background: #eee;
    line-height: 22px;
    padding-left: 10px;
    color: #666;
    font-size: 14px;
}

.border-topbottom::before{
    border-color: #ccc;
}
.border-topbottom::after{
    border-color: #ccc;
}

.button-list{
    overflow: hidden;
    padding: 5px 30px 5px 5px;
}
.button-wrapper{
    width: 33.3%;
    float: left;
}
.button{
    margin: 5px;
    padding: 5px 0;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 5px;
    /* overflow: hidden; */
    height: 15px;
}

.item{
    line-height: 38px;
    padding-left: 10px;
    border-bottom: 10px;
}
.border-bottom::after{
    border-color: rgb(7, 0, 0);
}

.alphabet{
    z-index: 999;
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 20px;
    position: absolute;
    top: 82px;
    bottom: 0;
    right: 0;
    text-align: center;
    line-height: 20px;
}

</style>
